package com.ruoyi.point.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.point.mapper.LogisticsMapper;
import com.ruoyi.point.mapper.ExchangeOrderMapper;
import com.ruoyi.point.domain.Logistics;
import com.ruoyi.point.domain.ExchangeOrder;
import com.ruoyi.point.service.ILogisticsService;
import com.ruoyi.point.service.IExchangeOrderService;

import java.util.Date;

/**
 * 物流信息Service业务层处理
 * 
 * @author hy
 * @date 2025-08-12
 */
@Service
public class LogisticsServiceImpl implements ILogisticsService 
{
    @Autowired
    private LogisticsMapper logisticsMapper;

    @Autowired
    private ExchangeOrderMapper exchangeOrderMapper;

    @Autowired
    private IExchangeOrderService exchangeOrderService;

    /**
     * 查询物流信息
     * 
     * @param id 物流信息主键
     * @return 物流信息
     */
    @Override
    public Logistics selectLogisticsById(Integer id)
    {
        return logisticsMapper.selectLogisticsById(id);
    }

    /**
     * 查询物流信息列表
     * 
     * @param logistics 物流信息
     * @return 物流信息
     */
    @Override
    public List<Logistics> selectLogisticsList(Logistics logistics)
    {
        return logisticsMapper.selectLogisticsList(logistics);
    }

    /**
     * 新增物流信息
     * 
     * @param logistics 物流信息
     * @return 结果
     */
    @Override
    public int insertLogistics(Logistics logistics)
    {
        logistics.setCreateTime(DateUtils.getNowDate());
        int result = logisticsMapper.insertLogistics(logistics);
        
        // 不再需要清除缓存
        return result;
    }

    /**
     * 修改物流信息
     * 
     * @param logistics 物流信息
     * @return 结果
     */
    @Override
    public int updateLogistics(Logistics logistics)
    {
        int result = logisticsMapper.updateLogistics(logistics);
        
        // 不再需要清除缓存
        return result;
    }

    /**
     * 批量删除物流信息
     * 
     * @param ids 需要删除的物流信息主键
     * @return 结果
     */
    @Override
    public int deleteLogisticsByIds(Integer[] ids)
    {
        return logisticsMapper.deleteLogisticsByIds(ids);
    }

    /**
     * 删除物流信息信息
     * 
     * @param id 物流信息主键
     * @return 结果
     */
    @Override
    public int deleteLogisticsById(Integer id)
    {
        return logisticsMapper.deleteLogisticsById(id);
    }

    /**
     * 查询物流信息列表（多表联查，包含订单信息）
     * 
     * @param logistics 查询条件
     * @return 物流信息集合
     */
    @Override
    public List<Logistics> selectLogisticsListWithOrder(Logistics logistics)
    {
        return logisticsMapper.selectLogisticsListWithOrder(logistics);
    }

    /**
     * 根据订单ID查询物流信息（多表联查）
     * 
     * @param orderId 订单ID
     * @return 物流信息集合
     */
    @Override
    public List<Logistics> selectLogisticsByOrderId(String orderId)
    {
        return logisticsMapper.selectLogisticsByOrderId(orderId);
    }

    /**
     * 根据用户ID查询物流信息（多表联查）
     * 
     * @param userId 用户ID
     * @return 物流信息集合
     */
    @Override
    public List<Logistics> selectLogisticsByUserId(Integer userId)
    {
        return logisticsMapper.selectLogisticsByUserId(userId);
    }

    /**
     * 根据地址ID查询物流信息（多表联查）
     * 
     * @param addressId 地址ID
     * @return 物流信息集合
     */
    @Override
    public List<Logistics> selectLogisticsByAddressId(Integer addressId)
    {
        return logisticsMapper.selectLogisticsByAddressId(addressId);
    }

    /**
     * 查询所有处理中的订单（用于物流管理）
     * 
     * @return 处理中订单列表
     */
    @Override
    public List<ExchangeOrder> selectProcessingOrdersForLogistics()
    {
        return exchangeOrderService.selectProcessingOrders();
    }

    /**
     * 新增物流信息并更新订单状态
     * 
     * @param logistics 物流信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLogisticsAndUpdateOrder(Logistics logistics)
    {
        // 设置物流状态为已发货
        logistics.setShippedStatus();
        logistics.setCreateTime(DateUtils.getNowDate());
        
        // 新增物流信息
        int result = logisticsMapper.insertLogistics(logistics);
        
        if (result > 0) {
            // 更新订单状态为已发货
            ExchangeOrder order = new ExchangeOrder();
            order.setId(logistics.getOrderId().longValue());
            order.setShippedStatus();
            
            int updateResult = exchangeOrderMapper.updateExchangeOrder(order);
            if (updateResult > 0) {
                System.out.println("订单状态已更新为已发货，订单ID: " + logistics.getOrderId());
                
                // 不再需要清除缓存
            } else {
                System.out.println("订单状态更新失败，订单ID: " + logistics.getOrderId());
            }
        }
        
        return result;
    }

    /**
     * 更新物流状态为已签收
     */
    @Override
    @Transactional
    public int updateLogisticsToReceived(Integer logisticsId)
    {
        Logistics logistics = new Logistics();
        logistics.setId(logisticsId);
        logistics.setReceivedStatus();
        
        int result = logisticsMapper.updateLogistics(logistics);
        
        if (result > 0) {
            // 获取物流信息以更新订单状态
            Logistics currentLogistics = logisticsMapper.selectLogisticsById(logisticsId);
            if (currentLogistics != null && currentLogistics.getOrderId() != null) {
                // 更新订单状态为已完成
                ExchangeOrder order = new ExchangeOrder();
                order.setId(currentLogistics.getOrderId().longValue());
                order.setCompletedStatus();
                
                int updateResult = exchangeOrderMapper.updateExchangeOrder(order);
                if (updateResult > 0) {
                    System.out.println("订单状态已更新为已完成，订单ID: " + currentLogistics.getOrderId());
                    
                    // 不再需要清除缓存
                } else {
                    System.out.println("订单状态更新失败，订单ID: " + currentLogistics.getOrderId());
                }
            }
        }
        
        return result;
    }
}
